<?php
namespace app\admin\controller;

class Type extends Common
{
    public function onetype_list()
    {   
        $ret = db('onetype')->order("addtime desc")->paginate(3);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',3);
        return view('onetype_list');
    }

    public function twotype_list()
    {   
        $ret = db('twotype')->alias("a")->join("onetype o","a.one_id=o.onetype_id")
        ->field("a.*,o.name as onetype_name")->order("addtime desc")->paginate(3);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',4);
        return view('twotype_list');
    }

    public function add_onetype(){
        $this->assign('pick',3);
        return view('add_onetype');
    }

    public function add_twotype(){
        $onetype_list = db('onetype')->select();
        $this->assign('onetype_list',$onetype_list);
        $this->assign('pick',4);
        return view('add_twotype');
    } 

    public function add_onetype_action(){
        $type_name     = input('post.type_name');
        $data = [
            'name'     => $type_name,
            'addtime'  => time()
        ]; 
        
        $ret = db('onetype')->insert($data);
        if($ret ==false)
        {
            $this->error("添加类型失败！");
        }
        $this->success("添加类型成功！",'/admin/type/onetype_list');
    }

    public function add_twotype_action(){
        $type_name     = input('post.type_name');
        $onetype_id     = input('post.onetype_id');
        $data = [
            'name'     => $type_name,
            'one_id'   => $onetype_id,
            'addtime'  => time()
        ]; 
        
        $ret = db('twotype')->insert($data);
        if($ret ==false)
        {
            $this->error("添加类型失败！");
        }
        $this->success("添加类型成功！",'/admin/type/twotype_list');
    }

    public function onetype_del()
    {
        $onetype_id = input('get.onetype_id');
        $ret = db('onetype')->where("onetype_id={$onetype_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }
    
    public function twotype_del()
    {
        $twotype_id = input('get.twotype_id');
        $ret = db('twotype')->where("twotype_id={$twotype_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }
    
}
